﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using Class_DTO;

namespace Class_DAO
{
    public class HopDongDAO
    {
        public  DataTable getHopDongQC()
        {
            DataProvider.GetConnection();
            SqlConnection cn = DataProvider.GetConnection();
            cn.Open();
            SqlDataAdapter ad = new SqlDataAdapter("getHopDong", cn);
            DataTable dt = new DataTable();
            ad.Fill(dt);
            cn.Close();
            return dt;

        }
        public DataTable getHopDong_theoten(string pTen)
        {
            DataProvider.GetConnection();
            SqlConnection cn = DataProvider.GetConnection();
            cn.Open();
            SqlDataAdapter ad = new SqlDataAdapter("[getHopDong_theoten]", cn);
            SqlCommand cmd = new SqlCommand("getHopDong_theoten", cn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@tenhd", SqlDbType.NVarChar);
            cmd.Parameters["@tenhd"].Value = pTen;
            ad.SelectCommand = cmd;
            DataTable dt = new DataTable();
            ad.Fill(dt);
            cn.Close();
            return dt;
        }

        public void ThemHopDong_DAO(HopDongDTO hdDTO)
        {
            DataProvider.GetConnection();
            SqlConnection cn = DataProvider.GetConnection();
            cn.Open();
            SqlCommand cmd = new SqlCommand("insertHDQC", cn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@mahd", SqlDbType.Int);
            cmd.Parameters.Add("@tenhd", SqlDbType.NVarChar);
            cmd.Parameters.Add("@ngayky", SqlDbType.DateTime);            
            cmd.Parameters.Add("@makh", SqlDbType.Int);
            cmd.Parameters.Add("@manv", SqlDbType.Int);
            cmd.Parameters.Add("@maqc", SqlDbType.Int);
           // cmd.Parameters.Add("@tongtien", SqlDbType.Float);
            cmd.Parameters["@mahd"].Direction = ParameterDirection.Output;

            cmd.Parameters["@tenhd"].Value = hdDTO.Tenhd;
            cmd.Parameters["@ngayky"].Value = hdDTO.Ngayky;
            cmd.Parameters["@makh"].Value = hdDTO.Makh;
            cmd.Parameters["@manv"].Value = hdDTO.Manv;
            cmd.Parameters["@maqc"].Value = hdDTO.Maqc;
            //cmd.Parameters["@tongtien"].Value = hdDTO.Tongtien ;
           
            cmd.ExecuteNonQuery();

            int mqc = Convert.ToInt32(cmd.Parameters["@mahd"].Value);
            hdDTO.Maqc = mqc;
            cn.Close();
        }
        // sua lam theo mo hinh khac
        public void sua(int mahd, string tenhd,DateTime ngayky, int makh, int manv, int maqc)
        {
            SqlParameter[] sp = new SqlParameter[6];
            sp[0] = new SqlParameter("@mahd", mahd);
            sp[1] = new SqlParameter("@tenhd", tenhd);
            sp[2] = new SqlParameter("@ngayky", ngayky);            
            sp[3] = new SqlParameter("@makh", makh);
            sp[4] = new SqlParameter("@manv", manv);
            sp[5] = new SqlParameter("@maqc", maqc);
            //sp[5] = new SqlParameter("@tongtien", tongtien );
            DataProvider.proc("updateHD", sp);
        }
        public void xoa(int s1)
        {
            SqlParameter[] sp = new SqlParameter[1];
            sp[0] = new SqlParameter("@mahd", s1);
            DataProvider.proc("deleteHopDong", sp);
        }

        
    }
}
